<template>
  <div class="add">
    <el-dialog
      :title="{ add: '新增企业', edit: '编辑企业' }[mode]"
      :visible.sync="isShow"
      width="600px"
      center
      @close="close"
    >
      <el-form
        ref="addForm"
        label-width="120px"
        :model="dataForm"
        :rules="rules"
      >
        <el-form-item label="企业编号" prop="eid">
          <el-input v-model="dataForm.eid" />
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="dataForm.name" />
        </el-form-item>
        <el-form-item label="企业简称" prop="short_name">
          <el-input v-model="dataForm.short_name" />
        </el-form-item>
        <el-form-item label="企业简介" prop="intro">
          <el-input v-model="dataForm.intro" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="dataForm.remark" />
        </el-form-item>
      </el-form>
      <template slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      isShow: false,
      mode: 'add',
      dataForm: {
        id: '',
        eid: '', //	string	企业编号
        name: '', //	string	企业名称
        short_name: '', //	string	简称
        intro: '', //	string	企业简介
        remark: '' //	string	备注
      },
      rules: {
        eid: [{ required: true, message: '请输入企业编号', trigger: 'change' }],
        name: [
          { required: true, message: '请输入企业名称', trigger: 'change' }
        ],
        short_name: [
          { required: true, message: '请输入企业简称', trigger: 'change' }
        ],
        intro: [
          { required: true, message: '请输入企业简介', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    async submit() {
      this.$refs.addForm.validate(async(valid) => {
        if (!valid) {
          return
        }
        if (this.mode === 'add') {
          await this.$api.enterprise.enterpriseAddPost(this.dataForm)
          this.$message.success('新增企业成功')
        } else if (this.mode === 'edit') {
          await this.$api.enterprise.enterpriseEditPost(this.dataForm)
          this.$message.success('编辑企业成功')
        }
        this.$emit('refresh')
        this.isShow = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  width: 90%;
}
</style>
